# -*- coding: utf-8 -*-
import time

from datetime import timedelta, datetime

# 1. 把一个自字符串格式的时间转化成结构化的时间
# 创建一个时间， 2018-10-01
t1 = time.strptime('2018-10-01', '%Y-%m-%d')
print(t1) # 结构化的时间
print(t1.tm_year)

#2. 把结构化的时间格式转换成指定格式字符串: 2018.10.01
t2 = time.localtime()
t3 = time.strftime('%Y.%m.%d', t2)
print(t3) # 2022.08.01

# 3. 时间间隔
t4 = datetime.now()
print(t4)
print(type(t4))
# 在t2的基础上再加一天
t5 = t4 + timedelta(days=1)
print(t5)


# 获取当前时间戳
now_time = time.time()
format_date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
print(format_date) # 2022-08-01

# 获取指定时间的前几天后几天
last_date = "2023-06-07 12:22:56"
date_format = "%Y-%m-%d %H:%M:%S"

three_before_day = (datetime.strptime(last_date, date_format) + timedelta(days=-2)).strftime(date_format)

print(three_before_day)  # 2023-06-05 12:22:56